#include <stdio.h>
#include "head.h"
int recvFile(int netfd){
    train_t train;
    char filename[4096] = {0};
    recvn(netfd,&train.length,sizeof(train.length));//收火车头
    recvn(netfd,train.data,train.length);//收车厢
    memcpy(filename,train.data,train.length);
    printf("%s\n",filename);

    int fd = open(filename,O_RDWR|O_CREAT|O_TRUNC,0666);
    ERROR_CHECK(fd,-1,"open");

    //获取当前文件大小
    struct stat statbuf;
    fstat(fd,&statbuf);
    off_t existing_size = statbuf.st_size;
    lseek(fd,0,SEEK_END);//定位到文件的末尾

    //接收文件总大小
    off_t filesize;
    recvn(netfd,&train.length,sizeof(train.length));
    recvn(netfd,train.data,train.length);
    memcpy(&filesize,train.data,train.length);
    printf("Filesize = %ld\n",filesize);
    printf("existing_size = %ld\n",existing_size);

    //发送已经接收的文件大小给发送方
    train.length = sizeof(existing_size);
    memcpy(train.data,&train.length,sizeof(train.length));
    send(netfd,&train.length,sizeof(train.length),MSG_NOSIGNAL);
    send(netfd,train.data,train.length,MSG_NOSIGNAL);

    //检查是否需要续传
    if(existing_size >=filesize){
        printf("已有相同文件。\n");
        close(fd);
        return 0;
    }
    off_t remaining =filesize - existing_size;
    printf("Remaining to transfer: %ld bytes\n",remaining);

    //大文件使用mmap
    if(filesize > MMAP_THRESHOLD){
        printf("检测到大文件（>100MB），使用mmap优化\n");
        ftruncate(fd,filesize);

        char *map = (char *)mmap(NULL,remaining,PROT_WRITE,MAP_SHARED,fd,existing_size);
        if(map == MAP_FAILED){
            perror("mmap failed");
            //使用普通传输
            lseek(fd,existing_size,SEEK_SET);
            off_t currsize = existing_size;
            while(remaining >0){
                recvn(netfd,&train.length,sizeof(train.length));
                if(train.length == 0) break;

                recvn(netfd,train.data,train.length);
                write(fd,train.data,train.length);
                currsize +=train.length;
                remaining -=train.length;
                printf("%5.2lf%%\r",100.0*currsize / filesize);
                fflush(stdout);

            }
        }else{
            printf("mmap Success\n");
            off_t received = 0;
            while(received <remaining){
                recvn(netfd,&train.length,sizeof(train.length));
                if(train.length ==0) break;

                recvn(netfd,map + received,train.length);
                received +=train.length;
                printf("%5.2lf%%\r", 100.0 * (existing_size + received) / filesize);
                fflush(stdout);
            }
            munmap(map,remaining);
        }
    }else{
        printf("小文件，使用正常传输\n");
        off_t currsize = existing_size;
        while(remaining>0){
            recvn(netfd,&train.length,sizeof(train.length));
            if(train.length == 0) break;
            recvn(netfd,train.data,train.length);
            currsize +=train.length;
            remaining -=train.length;
            printf("%5.2lf%%\r", 100.0 * currsize / filesize);
            fflush(stdout);
        }
    }
    printf("100.00%%\n");
    close(fd);
    return 0;
}

int main(int argc, char *argv[]){
    return 0;
}

